package org.vaadinpa.data;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;

import org.vaadinpa.gui.VaadinPA;
import org.vaadinpa.logic.Command;
import org.vaadinpa.logic.CommandHandler;
import org.vaadinpa.logic.Ticker;

public class Universe extends HashMap<Coordinate, Planet> {

	private static final long serialVersionUID = 1969691880162470704L;

	public static Universe instance = new Universe();

	public String serialize() {

		StringBuilder sb = new StringBuilder();

		sb.append("<tick>" + Ticker.currentTick + "</tick>\n");
		sb.append("<serverID>" + VaadinPA.SERVER_INSTANCE_ID + "</serverID>\n");

		sb.append("<planets>");
		Collection<BattleNewsItem> battles = new HashSet<BattleNewsItem>();
		for (Entry<Coordinate, Planet> e : entrySet()) {
			e.getValue().serialize(sb, battles);
		}
		sb.append("</planets>");

		sb.append("<battles>");
		for (BattleNewsItem c : battles) {
			c.serialize(sb);
		}
		sb.append("</battles>");

		sb.append("<commands>");
		for (Command c : CommandHandler.getWaitingCommands()) {
			c.serialize(sb);
		}
		sb.append("</commands>");

		return sb.toString();

	}

}
